package com.sdd.rpi_mobile.eventRss;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.sdd.rpi_mobile.R;

/** Activity to display the list of different RSS feeds to choose from. */
public class RssFeedChooserActivity extends Activity {
	
	/**
	 * Private class used to give the right parameters for 
	 * the rssFeeds.add method, adding the items to the list.
	 */
	private class RssFeed {
		public String name;
		public URL url;
		
		public RssFeed(String name, URL url){
			this.name = name;
			this.url = url;
		}
	}
	
	private List<RssFeed> mRssFeeds = new ArrayList<RssFeed>();

	/** Shows a list of RSS feeds available to the user */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rssfeedchooser);
		
		try {
			mRssFeeds.add(new RssFeed("Events", new URL("http://events.rpi.edu/webcache/v1.0/rssDays/7/list-rss/no--filter.rss")));
			mRssFeeds.add(new RssFeed("Sports", new URL("http://www.rpiathletics.com/calendar.ashx/calendar.rss?sport_id=&han=")));
			mRssFeeds.add(new RssFeed("EMPAC", new URL("http://events.rpi.edu/webcache/v1.0/rssDays/7/list-rss/%28catuid%3D%2700f18254-27fe1f37-0127-fe1f4be6-00000111%27-_catuid%3D%2700f18254-2c8b5503-012c-efcae63a-000073cb%27-_catuid%3D%2700f18254-2c8b5503-012c-efca7282-000073bb%27-_catuid%3D%2700f18254-2c8b5503-012c-efcb39d6-000073cd%27%29.rss")));
		} catch (MalformedURLException e) {
			//Ignore item, we can't fix the URL
		}
		
		List<String> feedNameList = new ArrayList<String>();
		for(RssFeed feed: mRssFeeds) feedNameList.add(feed.name);

		final ListView list= (ListView) findViewById(R.id.rss_feed_chooser);
		list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, feedNameList));
		list.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
				Bundle bundle = new Bundle();
				ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
				if(networkInfo != null && networkInfo.isConnected()) {
					bundle.putSerializable("rss_url", mRssFeeds.get(pos).url);
					Intent intent = new Intent(view.getContext(), RssFeedActivity.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
				else {
					Toast notification = Toast.makeText(view.getContext(), 
							"Unable to connect", Toast.LENGTH_LONG);
					notification.show();
				}
			}			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.rss_feed_chooser, menu);
		return true;
	}
}
